package com.padakeji.android.pworks.module.version;

import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import android.widget.Toast;
import com.padakeji.android.pworks.content.IntentAction;
import com.padakeji.android.pworks.module.version.entity.VersionInfo;
import com.padakeji.android.pworks.module.version.view.UpdateDialogActivity;
import com.padakeji.android.pworks.module.version.view.UpdateDialogFragment;
import com.padakeji.android.pworks.ui.UIHelper;

public abstract class VersionController {

	public static final String ARGS_KEY_VERSION_CODE = "versionCode";
	public static final String ARGS_KEY_VERSION_NAME = "versionName";
	public static final String ARGS_KEY_DOWNLOAD_LINK = "downloadLink";
	public static final String ARGS_KEY_RELEASE_NOTES = "releaseNotes";


	public static interface VersionCheckListener {
		void onNewVersionChecked(VersionInfo versionInfo);

		void noNewVersionChecked(boolean isManual);
	}


	public static class Environment {

	}

	private Context mContext;

	private VersionCheckListener mVersionCheckListener;

	private PackageInfo mPackageInfo;

	public VersionController(final Context context) {

		this.mContext = context;
		this.mPackageInfo = getPackageInfo();
		this.mVersionCheckListener = new VersionCheckListener() {
			@Override
			public void onNewVersionChecked(VersionInfo newVersionInfo) {
				Intent i = new Intent(mContext, UpdateDialogActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra(ARGS_KEY_VERSION_CODE, newVersionInfo.versionCode);
                i.putExtra(ARGS_KEY_VERSION_NAME, newVersionInfo.versionName);
                i.putExtra(ARGS_KEY_DOWNLOAD_LINK, newVersionInfo.downloadLink);
                i.putExtra(ARGS_KEY_RELEASE_NOTES, newVersionInfo.releaseNotes);
				context.startActivity(i);
			}

			@Override
			public void noNewVersionChecked(boolean isManual) {
                new UIHelper(mContext).showToast("当前版本是最新版本", Toast.LENGTH_LONG);
			}
		};
	}

	public void checkUpdate(boolean isManual) {
		VersionInfo remoteVersionInfo = checkRemoteVersion();
		boolean needUpdate = remoteVersionInfo != null && remoteVersionInfo.versionCode > currentVersionCode();
		if (needUpdate) {
			sendUpdateBroadcast(remoteVersionInfo);
            if (mVersionCheckListener != null) {
                mVersionCheckListener.onNewVersionChecked(remoteVersionInfo);
            }
		} else if (mVersionCheckListener != null) {
            mVersionCheckListener.noNewVersionChecked(isManual);
        }
	}

	public abstract VersionInfo checkRemoteVersion();


	public int currentVersionCode() {
		return mPackageInfo.versionCode;
	}

	public String currentVersionName() {
		return mPackageInfo.versionName;
	}

	public void setOnNewVersionCheckedListener(VersionCheckListener l) {
		this.mVersionCheckListener = l;
	}

	private PackageInfo getPackageInfo() {
		try {
			PackageInfo pi = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
			return pi;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void sendUpdateBroadcast(VersionInfo newVersionInfo) {
		Intent i = new Intent(IntentAction.VERSION_UPDATE);

		i.putExtra(ARGS_KEY_VERSION_CODE, newVersionInfo.versionCode);
		i.putExtra(ARGS_KEY_VERSION_NAME, newVersionInfo.versionName);
		i.putExtra(ARGS_KEY_DOWNLOAD_LINK, newVersionInfo.downloadLink);
		i.putExtra(ARGS_KEY_RELEASE_NOTES, newVersionInfo.releaseNotes);

		mContext.sendBroadcast(i);
	}

}

